// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../Commons/GameEvent";
import { getHost, NetManager } from "../Managers/NetManager";
import SaveManager from "../Managers/SaveManager";
import { GAME_ID } from "../Platforms/PBPlatform";
import PlatformManager from "../Platforms/PlatformManager";
import NotIndulgeView from "../UIs/UIs/NotIndulgeView";
import WebUI from "../UIs/UIs/WebUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoginSceneComp extends cc.Component {

    @property(cc.Button)
    startBtn: cc.Button = null;
    @property(cc.Button)
    continueBtn: cc.Button = null;
    @property(cc.Label)
    loginTxt: cc.Label = null;
    @property(cc.Node)
    bg: cc.Node = null;
    @property(cc.Node)
    loginNode: cc.Node = null;
    @property(cc.EditBox)
    accountEdit: cc.EditBox = null;
    @property(cc.EditBox)
    passwordEdit: cc.EditBox = null;
    @property(cc.Label)
    tipsTxt: cc.Label = null;
    @property(cc.Button)
    loginBtn: cc.Button = null;
    @property(cc.Node)
    loginIng: cc.Node = null;
    @property(cc.Button)
    registerBtn: cc.Button = null;
    @property(NotIndulgeView)
    notIndulgeView: NotIndulgeView = null;
    @property(WebUI)
    webUI: WebUI = null;
    @property(cc.Toggle)
    agreeToggle: cc.Toggle = null;
    @property(cc.Node)
    ageUI: cc.Node = null;

    protected onLoad(): void {
        this.webUI.node.active = false;
        this.ageUI.active = false;
    }
    protected onEnable(): void {
        GameEvent.eventNode.on(GameEvent.LOADSDKCOMPLETE_EVENT, this.onLoadSdk, this);

        // if (SaveManager.single.isHavSaveData()) {
        //     this.continueBtn.interactable = true;
        // } else {
        //     this.continueBtn.interactable = false;
        // }
        this.startBtn.node.active = false;
        this.continueBtn.node.active = false;
        this.loginIng.active = false;
        this.loginNode.active = false;
        this.tipsTxt.node.active = false;
        this.notIndulgeView.node.active = false;

        this.loginTxt.string = "正在加载······";
        if (cc.sys.localStorage.getItem("isAgreeAgreement") != null) {
            var isAgree: string = cc.sys.localStorage.getItem("isAgreeAgreement");
            let boolValue = (/true/).test(isAgree);
            this.agreeToggle.isChecked = boolValue;
        } else {
            this.agreeToggle.isChecked = false;
        }
    }
    protected onDisable(): void {
        GameEvent.eventNode.off(GameEvent.LOADSDKCOMPLETE_EVENT, this.onLoadSdk, this);
    }
    onLoadSdk() {
        this.bg.active = false;
        this.loginTxt.node.active = false;
        this.loginNode.active = true;

        if (cc.sys.localStorage.getItem("accountName") != null) {
            var account = cc.sys.localStorage.getItem("accountName");
            var password = cc.sys.localStorage.getItem("passwodName");
            this.accountEdit.string = account;
            this.passwordEdit.string = password;
        }


    }

    public startGameClick() {
        SaveManager.single.initUserData();
        this.loadScene();
    }

    public continueGameClick() {
        SaveManager.single.loadData();
        this.bg.active = true;
        this.loginTxt.node.active = true;
        this.loginTxt.string = "正在加载场景······";
        this.loadScene();
    }


    loadScene() {
        var that = this;
        cc.director.preloadScene("Scene_Load", (completedCount: number, totalCount: number, item: any) => {
            that.loginTxt.string = "加载场景：" + completedCount + "/" + totalCount;
        }, (error: Error) => {

        });
        cc.director.loadScene("Scene_Load");
    }
    showTips(str: string) {
        this.unscheduleAllCallbacks();
        this.tipsTxt.node.active = true;
        this.tipsTxt.string = str;
        this.scheduleOnce(() => {
            this.tipsTxt.node.active = false;
        }, 1);
    }
    showAgeUI() {
        this.ageUI.active = true;
    }

    agreeToggleClick() {
        cc.sys.localStorage.setItem("isAgreeAgreement", this.agreeToggle.isChecked);
    }
    async loginClick() {
        if (!this.agreeToggle.isChecked) {
            this.showTips("请阅读并勾选同用户协议和隐私保护指引。");
            return;
        }

        var account = this.accountEdit.string;
        if (account.length == 0) {
            this.showTips("请输入账号。");
            return;
        }

        var password = this.passwordEdit.string;
        if (password.length == 0) {
            this.showTips("请输入密码。");
            return;
        }
        this.loginBtn.node.active = false;
        this.registerBtn.node.active = false;
        this.loginIng.active = true;
        let result = await PlatformManager.single.basePlatform.sdk.login(account, password);
        if (result.code == 200) {
            console.log('登录成功', result);
            this.showTips(result.message);
            SaveManager.single.age = result.data.age;
            SaveManager.single.token = result.data.token;
            SaveManager.single.accountData.init(result.data.account);

            cc.sys.localStorage.setItem("accountName", account);
            cc.sys.localStorage.setItem("passwodName", password);
            this.notIndulgeView.node.active = SaveManager.single.accountData.realName != null && SaveManager.single.accountData.realName.length == 0;
            this.loginNode.active = false;
            this.bg.active = true;
            this.loginTxt.node.active = true;
            this.loginTxt.string = "正在获取游戏数据······";
            this.getGameData();
        } else {
            this.loginIng.active = false;
            this.loginBtn.node.active = true;
            this.registerBtn.node.active = true;
            console.log('登录失败', result);
            this.showTips(result.message);
        }
    }
    async registerClick() {
        var account = this.accountEdit.string;
        if (account.length == 0) {
            this.showTips("请输入账号。");
            return;
        }

        var password = this.passwordEdit.string;
        if (password.length == 0) {
            this.showTips("请输入密码。");
            return;
        }
        this.registerBtn.interactable = false;
        let result = await PlatformManager.single.basePlatform.sdk.register(account, password)
        if (result.code == 200) {
            this.registerBtn.interactable = true;
            console.log('注册成功', result);
            this.showTips("注册成功。");
            SaveManager.single.accountData.init(result.data.account);
        } else {
            this.registerBtn.interactable = true;
            console.log('注册失败', result);
            this.showTips(result.message);
        }
    }
    async getGameData() {
        var accountData = SaveManager.single.accountData;
        let result = await PlatformManager.single.basePlatform.sdk.getGameData(accountData.username);
        if (result.code == 200) {
            console.log('获取游戏数据成功', result);
            this.bg.active = false;
            this.loginTxt.node.active = false;
            // this.startBtn.node.active = true;
            this.continueBtn.node.active = true;
            if (result.data != null && result.data.length > 0) {
                SaveManager.single.cloudSaveData = result.data[0];
            }
        } else {
            console.log('获取游戏数据失败', result);
            this.bg.active = false;
            this.loginTxt.node.active = false;
            // this.startBtn.node.active = true;
            this.continueBtn.node.active = true;
        }
    }

    /**隐私保护请求
     */
    userPrivacyAgreementRequest() {
        let url: string = getHost() + "/privacy/userPrivacyAgreement/" + GAME_ID;
        this.webUI.showUI("隐私保护引导", url);
        // NetManager.Instance.doHttp(url, null, "GET", cb);
    }
    /**用户协议 */
    userAgreementRequest() {
        let url: string = getHost() + "/privacy/userAgreement/" + GAME_ID;
        this.webUI.showUI("用户协议", url);
        // NetManager.Instance.doHttp(url, null, "GET", cb);
    }
}
